<?php
class Member_memberModel extends Model {
	
	public function _list($view,$map,$sort='mm.deep',$pagesize=10){
		//计算总页数
		//$map['n.pid'] = 0;
		$count=$this->alias('mm')-> join("JOIN __MEMBER__ AS m on mm.smember_id = m.id") -> where($map) -> count();
		$memberdb = D('Member');
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this-> alias('mm') -> join("JOIN __MEMBER__ AS m on mm.smember_id = m.id") -> where($map)->order($sort)->limit($p->firstRow.','.$p->listRows)->select();
        $pages=$p->show();
		$msaledb = M('Membermembersale'); 
		$saledb = M('Sale');
		foreach($list as $k => $v){
			$list[$k]['levelname'] = $memberdb -> getLevelName($v['id']);
			$list[$k]['consume'] = $saledb -> where(array('member_id' => $list[$k]['id'], 'pay_status' => 1)) -> sum('total');
			$list[$k]['record'] = $msaledb -> where(array('member_id' => $list[$k]['id'], 'pay_status' => 1)) -> sum('total');
			$list[$k]['shopname'] = M('Supplier')  -> where(array('member_id' => $list[$k]['id'])) -> getField('susername');
			$list[$k]['money']=D('Shop_turnover')->where('shop_id = '.$list[$k]['smember_id'])->sum('total');
			$list[$k]['discounted']=D('Member_account_log')->where('from_member = '.$list[$k]['smember_id'].' and types = 2 and status = 1')->sum('changenum');
			//echo $msaledb -> getLastSql();
			//dump(array('member_id' => $list[$k]['id'], 'pay_status' => 1));
		}
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
}